/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * IHMSimulatorAddRequestDialog.java
 *
 * Created on 15 janv. 2011, 21:36:17
 */

package IHM;

import EcoSpeed.Utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Pierre
 */
public class IHMSimulatorAddRequestDialog extends javax.swing.JDialog {

    public enum MODE { NORMAL, DIRECT }

    private IHMSimulator parent;
    private MODE mode;

    /** Creates new form IHMSimulatorAddRequestDialog */
    public IHMSimulatorAddRequestDialog(IHMSimulator parent, boolean modal) {
        super(parent, modal);
        this.parent = parent;
        initComponents();
    }

    public void showPopup(MODE mode)
    {
        this.mode = mode;

        // Si c'est un mode normal, j'initialise le champs date
        // avec le max des jeux d'essai.
        if(mode == MODE.NORMAL){
            if(jTF_Date.getText().isEmpty()){
                    jTF_Date.setText(parent.getRequestMaxDate());
            }
        }
        
        // Si c'est un mode direct, j'initialise le champs date
        // avec la date courante de la simulation.
        else if (mode == MODE.DIRECT) {
            jTF_Date.setText(parent.getCurrentSimulationDate());
        }


        jTF_EndFloor.setText("");
        jTF_StartFloor.setText("");
        jTF_Weight.setText("70");
        jCB_Priority.setSelected(false);

        this.setLocationRelativeTo(parent);
        this.setVisible(true);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton_OK = new javax.swing.JButton();
        jButton_Cancel = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jCB_Priority = new javax.swing.JCheckBox();
        jTF_Weight = new javax.swing.JTextField();
        jTF_Date = new javax.swing.JTextField();
        jTF_StartFloor = new javax.swing.JTextField();
        jTF_EndFloor = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Ajout d'une requête");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButton_OK.setText("OK");
        jButton_OK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_OKActionPerformed(evt);
            }
        });
        getContentPane().add(jButton_OK, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 210, 90, -1));

        jButton_Cancel.setText("Annuler");
        jButton_Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CancelActionPerformed(evt);
            }
        });
        getContentPane().add(jButton_Cancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 210, 90, -1));

        jLabel1.setText("Date :");
        jLabel1.setPreferredSize(new java.awt.Dimension(30, 20));
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 50, 140, 25));

        jLabel2.setText("Etage d'appel :");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 80, 140, 25));

        jLabel3.setText("Etage de destination :");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 110, 140, 25));

        jLabel4.setText("Poids :");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 140, 140, 25));

        jCB_Priority.setText("Prioritaire");
        getContentPane().add(jCB_Priority, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 170, 120, 25));

        jTF_Weight.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        getContentPane().add(jTF_Weight, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 140, 180, 25));

        jTF_Date.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        getContentPane().add(jTF_Date, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 50, 180, 25));

        jTF_StartFloor.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        getContentPane().add(jTF_StartFloor, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 80, 180, 25));

        jTF_EndFloor.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        getContentPane().add(jTF_EndFloor, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 110, 180, 25));

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel5.setText("Ajout d'une nouvelle demande");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 10, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_OKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_OKActionPerformed

        // Une des zones n'est pas remplie.
        if(jTF_Date.getText().isEmpty() || 
           jTF_StartFloor.getText().isEmpty() || 
           jTF_EndFloor.getText().isEmpty() ||
           jTF_Weight.getText().isEmpty())
        {
            JOptionPane.showMessageDialog(this, "Vous devez remplir tous les champs du formulaire.");
        }
        
        else 
        {
            try{
                
                try {
                    new SimpleDateFormat("MM-dd-yyyy HH:mm:ss").parse(jTF_Date.getText());
                } 
                catch (ParseException ex) {
                    throw new Exception("Le format de date n'est pas valide, il faut un format MM-dd-yyyy HH:mm:ss.");
                }
                
                try {
                    int floor = Integer.parseInt(jTF_StartFloor.getText());
                    
                    // Si l'étage n'est pas valide.
                    if(floor > Utils.NB_FLOORS - Utils.NB_UNDERGROUND - 1 || floor < -Utils.NB_UNDERGROUND)
                        throw new Exception();
                } 
                catch (Exception ex) {
                    throw new Exception("L'étage d'appel doit être un entier compris dans les numéros d'étages disponibles. " +
                                        "(De " + (-Utils.NB_UNDERGROUND)+ " à " + (Utils.NB_FLOORS - Utils.NB_UNDERGROUND - 1) + ")");
                }

                try {
                    int floor = Integer.parseInt(jTF_EndFloor.getText());

                    // Si l'étage n'est pas valide.
                    if(floor > Utils.NB_FLOORS - Utils.NB_UNDERGROUND - 1 || floor < -Utils.NB_UNDERGROUND)
                        throw new Exception();
                }
                catch (Exception ex) {
                    throw new Exception("L'étage de destination doit être un entier compris dans les numéros d'étages disponibles. " +
                                        "(De " + (-Utils.NB_UNDERGROUND)+ " à " + (Utils.NB_FLOORS - Utils.NB_UNDERGROUND - 1) + ")");
                }

                try {
                    int weight = Integer.parseInt(jTF_Weight.getText());

                    // Si le poids est négatif
                    if(weight<0)
                        throw new Exception();
                }
                catch (Exception ex) {
                    throw new Exception("Le poids de la demande doit être un entier positif.");
                }

                if(mode == MODE.NORMAL){
                    parent.addNewRequest(jTF_Date.getText(), jTF_StartFloor.getText(), jTF_EndFloor.getText(), jTF_Weight.getText(), jCB_Priority.isSelected());
                }
                else if(mode == MODE.DIRECT){
                    parent.addDirectNewRequest(jTF_Date.getText(), jTF_StartFloor.getText(), jTF_EndFloor.getText(), jTF_Weight.getText(), jCB_Priority.isSelected());
                }

                this.setVisible(false);
            }
            catch(Exception ex)
            {
                JOptionPane.showMessageDialog(this, ex.getMessage());
            }
        }
}//GEN-LAST:event_jButton_OKActionPerformed

    private void jButton_CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_CancelActionPerformed
        this.setVisible(false);
}//GEN-LAST:event_jButton_CancelActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_Cancel;
    private javax.swing.JButton jButton_OK;
    private javax.swing.JCheckBox jCB_Priority;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField jTF_Date;
    private javax.swing.JTextField jTF_EndFloor;
    private javax.swing.JTextField jTF_StartFloor;
    private javax.swing.JTextField jTF_Weight;
    // End of variables declaration//GEN-END:variables

}
